#! /usr/bin/env ruby
$LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), 'lib'))

require 'json'
require 'panoptimon-collector-http'

ARGV[0] or raise "arguments required"
conf = JSON.parse(ARGV[0], {symbolize_names: true})

defaults = {
  method:  conf[:default_method] || 'head',
  timeout: conf[:default_timeout] || 3,
}

raise "must have 'urls' value in config" unless conf[:urls]
setup = conf[:urls].map {|u|
  o = defaults.merge(u.is_a?(Hash) ? u : {url: u})
  o[:url] = 'http://' . o[:url] unless o[:url].match(%r{^\w+://})
  o[:name] ||= o[:url].to_s
  o
}

output = Hash[setup.map {|o|
  [o[:name], Panoptimon::Collector::HTTP.new(o[:url], o).go]
}]
puts JSON.generate(output)

